package test5.lupeng.jf.com.teamdemo.activitys.found;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.adapter.NearListAdapter;
import test5.lupeng.jf.com.teamdemo.bean.request.NearPeopleRequest;
import test5.lupeng.jf.com.teamdemo.bean.response.NearPeopleBean;
import test5.lupeng.jf.com.teamdemo.untils.AppConfigUtils;
import test5.lupeng.jf.com.teamdemo.untils.RequestUtils;
import test5.lupeng.jf.com.teamdemo.untils.UserInfo;


//附近的人
public class NearPeopleActivity extends AppCompatActivity {

    @BindView(R.id.ic_back)
    ImageView icBack;
    @BindView(R.id.rv_near)
    RecyclerView rvNear;
    private String TAG = "NearPeopleActivity";
    private ArrayList<NearPeopleBean.ResultBean> mResult;
    private NearListAdapter nearListAdapter;
    private NearPeopleBean resultBean;
    private NearPeopleRequest nearPeopleRequest;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_near_people);
        ButterKnife.bind(this);
        mResult = new ArrayList<>();
        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        String userId = sp.getString("objectId", "0");
        nearPeopleRequest = new NearPeopleRequest(userId, 10, "");

        rvNear.setLayoutManager(new LinearLayoutManager(this));
        nearListAdapter = new NearListAdapter(mResult);
        rvNear.setAdapter(nearListAdapter);
        initRefreshLyout();
        getData1(true);
    }

    private void initRefreshLyout(){
        RefreshLayout refreshLayout= (RefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
                nearPeopleRequest.setTimestamp(0);
                getData1(true);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                getData1(false);
            }
        });
    }

    public void getData1(final boolean isRefresh){
        OkHttpClient client=new OkHttpClient();
        String url="https://leancloud.cn:443/1.1/functions/getUserList";


        Gson gson = new Gson();
        String json = gson.toJson(nearPeopleRequest);
        RequestBody body=RequestBody.create(RequestUtils.JSONTYPE,json);
        Request request= RequestUtils.getBuilder()
                .url(url)
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.i(TAG, "onResponse: " + result);

                resultBean = new Gson().fromJson(result,NearPeopleBean.class);
                if (isRefresh){
                    refreshData(resultBean);
                }
                // mResult.addAll(resultBean.getResult());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        nearListAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    public void refreshData(NearPeopleBean resultBean){
        mResult.clear();
        if (resultBean!=null){
            mResult.addAll(resultBean.getResult());

        }
        nearPeopleRequest.setTimestamp(0);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                nearListAdapter.notifyDataSetChanged();
            }
        });
    }

    @OnClick(R.id.ic_back)
    public void onViewClicked() {
        finish();
    }

}
